//
//  MTOpenRedpackDetailViewController.swift
//  MT
//
//  Created by ooops on 2019/12/23.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import YPNavigationBarTransition
import MTCategory

class MTOpenRedpackDetailViewController: BaseViewController {
    
    private var openRedpackModel: MTOpenRedpackModel?
    
    private let headerHeight: CGFloat = 266 + NavigationBarHeight
    
    override init() {
        super.init()
    }
    
    convenience init(_ openRedpackModel: MTOpenRedpackModel) {
        self.init()
        self.openRedpackModel = openRedpackModel
        self.headerView.openRedpackModel = openRedpackModel
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    required public init(rounterParams params: Dictionary<String, Any>?) {
        fatalError("init(rounterParams:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.text.x1d1a1a, NSAttributedString.Key.font:PingFangSCSemiboldFont(16)]
        self.title = "红包领取详情"
        setupNavigationBar()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        //adjustTableViewColor()
    }
    
    private func setupNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: self.backBtn)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: self.rightBtn)
    }
    
    lazy var backBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "nav_back"), for: .normal)
        btn.addTarget(self, action: #selector(back), for: .touchUpInside)
        return btn
    }()
    
    lazy var rightBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("红包记录", for: .normal)
        btn.addTarget(self, action: #selector(redpackHistory), for: .touchUpInside)
        btn.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
        btn.titleLabel?.font = PingFangSCMediumFont(16)
        return btn
    }()
    
    lazy var bgColorView: UIView = {
        let v = UIView(frame: view.bounds)
        v.backgroundColor = UIColor(hex: 0xF35C4B)
        return v
    }()
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: .zero, style: .plain)
        table.delegate = self
        table.dataSource = self
        table.register(MTOpenRedpackDetailCell.self, forCellReuseIdentifier: MTOpenRedpackDetailCell.reuseIdentity())
        table.separatorStyle = .none
        table.tableFooterView = UIView()
        table.backgroundColor = UIColor(hex: 0xF4F4F4)
        table.tableHeaderView = headerView
        if #available(iOS 11, *) {
            table.contentInsetAdjustmentBehavior = .never
        }
        return table
    }()
    
    lazy var headerView: MTOpenRedpackDetailHeaderView = {
        let v = MTOpenRedpackDetailHeaderView()
        v.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 310 + NavigationBarHeight)
        //v.openRedpackModel = self.openRedpackModel
//        v.backgroundColor = .white
        return v
    }()
}

extension MTOpenRedpackDetailViewController {
    
    @objc private func redpackHistory() {
        let nav = BaseNavController(rootViewController: MTOpenRedpackHistoryViewController())
//        let presentTransition: MTModalTransition = MTModalTransition()
//        nav.transitioningDelegate = presentTransition
        nav.modalPresentationStyle = .fullScreen
//        nav.modalTransitionStyle = .crossDissolve
        present(nav, animated: true)
    }
    
    @objc private func back() {
        navigationController?.popViewController(animated: true)
    }
    
}

extension MTOpenRedpackDetailViewController: UITableViewDelegate, UITableViewDataSource {
    
    //MARK: UITableViewDataSource
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.openRedpackModel?.result?.lists?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MTOpenRedpackDetailCell.reuseIdentity(), for: indexPath) as! MTOpenRedpackDetailCell
        configureCell(cell: cell, forRowAt: indexPath)
        return cell
    }
    
    func configureCell(cell: MTOpenRedpackDetailCell, forRowAt indexPath: IndexPath) {
        if let listModel: MTOpenRedpackResultListModel = self.openRedpackModel?.result?.lists?[indexPath.row] {
            cell.listModel = listModel
        }
    }
    
    //MARK: UITableViewDelegate
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
    
}


extension MTOpenRedpackDetailViewController {
    
    override func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        return [YPNavigationBarConfigurations.backgroundStyleTransparent]
    }
}
